package com.fuxin.app.web;

import com.fuxin.app.error.BusinessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/web")
public class WebServer {


    @RequestMapping("/hello")
    public ModelAndView hello(){
        Map<String,Object> map = new HashMap<>();
        map.put("message","hello world");
        map.put("time", new Date());
        map.put("list1", Arrays.asList("tom", "cat", "dog"));

        ModelAndView model = new ModelAndView("hello", map);
        return model;
    }

	@RequestMapping("/hello2")
	public String hello2(HttpServletRequest request){
		request.setAttribute("message","hello world 2");
		request.setAttribute("time", new Date());
		request.setAttribute("list1", Arrays.asList("tom", "cat", "dog"));
		return "hello";
	}

	@RequestMapping("/hello3")
	public String hello3(Map<String, Object> map){
		map.put("message","hello world3");
		map.put("time", new Date());
		map.put("list1", Arrays.asList("tom", "cat", "dog"));
		return "hello";
	}

	@RequestMapping("testError")
	public Map<String, String> testError(HttpSession session){
		Map<String, String> map = new HashMap<>();
		if(true){
			throw new BusinessException(403, "测试错误");
		}
		return map;
	}

}
